In [1]:
import sys
import os
sys.path.insert(0, os.path.abspath('../'))
sys.path.insert(0, os.path.abspath('../../'))
sys.path.insert(0, os.path.abspath('/home/hm-tlacherm/qlm_notebooks/notebooks_1.2.1/notebooks/master_thesis_qaoa/'))
sys.path.insert(0, os.path.abspath('/home/hm-tlacherm/qlm_notebooks/notebooks_1.2.1/notebooks/master_thesis_qaoa/ibm/'))
In [2]:
import numpy as np

import qiskit
provider = qiskit.IBMQ.load_account()
from qiskit import Aer
from qiskit.utils import QuantumInstance
from qiskit_optimization.algorithms import MinimumEigenOptimizer
from qiskit.algorithms import QAOA
from shared.QiskitMaxcut import *
from ibm.ibm_parameters import *

from matplotlib import pyplot as plt
%matplotlib inline

from ibm_landscape_processes import *
ibmqfactory.load_account:WARNING:2021-08-21 11:14:39,639: Credentials are already in use. The existing account in the session will be replaced.
In [3]:
%load_ext autoreload
%autoreload 2
In [4]:
# ---- Define graph and MaxCut ----
graph = load_nx_graph_from("/home/hm-tlacherm/qlm_notebooks/notebooks_1.2.1/notebooks/master_thesis_qaoa/data/graphs/16_nodes/graph_16_33_01_w.txt")
max_cut = Maxcut(graph)
max_cut_qubo = max_cut.to_qubo()
max_cut.draw()
In [5]:
# ---- Define step size and gamma, beta values ----
step_size = 0.1
a_gamma = np.arange(0, np.pi, step_size)
b_beta = np.arange(0, np.pi, step_size)
In [6]:
gammas, betas = np.meshgrid(a_gamma, b_beta)
In [7]:
# ---- execute for all values QAOA and get result matrix  ----
landscape = run_all(gammas, betas, max_cut)
Row 0
Row 1
Row 2
Row 3
Row 4
Row 5
Row 6
Row 7
Row 8
Row 9
Row 10
Row 11
Row 12
Row 13
Row 14
Row 15
Row 16
Row 17
Row 18
Row 19
Row 20
Row 21
Row 22
Row 23
Row 24
Row 25
Row 26
Row 27
Row 28
Row 29
Row 30
Row 31
In [8]:
print(landscape)
plt.matshow(landscape)
plt.show()
[[ -88.68000031  -88.41312408  -88.44975281 ...  -88.63487244
   -88.69625092  -88.27649689]
 [ -88.89787292  -77.05825043  -67.4103775  ... -112.84649658
  -104.28937531  -93.19237518]
 [ -88.53012848  -86.06424713  -83.65562439 ...  -93.71299744
   -91.95024872  -89.18399811]
 ...
 [ -88.67462158  -89.36199951  -90.9123764  ...  -85.93887329
   -86.56300354  -88.26850128]
 [ -88.38899994  -88.44400024  -87.91812134 ...  -89.91925049
   -88.72899628  -88.71437836]
 [ -88.90325165  -88.15087128  -88.30112457 ...  -89.00187683
   -88.93262482  -88.26287842]]
In [9]:
# Mean of landscape
np.mean(landscape)
Out[9]:
-88.49827872216702
In [10]:
# Minimium 
np.min(landscape)
Out[10]:
-118.09700012207031
In [11]:
# Display Coordinates of Minimum 
np.unravel_index(np.argmin(landscape), landscape.shape)
Out[11]:
(1, 12)
In [12]:
# Gamma and beta value of Minimium
gamma, beta = np.unravel_index(np.argmin(landscape), landscape.shape)
opt_gamma = gamma * step_size
opt_beta = beta * step_size
print(f"Opt.Gamma: {opt_gamma}, Opt.Beta: {opt_beta}")
Opt.Gamma: 0.1, Opt.Beta: 1.2000000000000002
In [13]:
# Save result matrix 
with open('landscape_simulator_paper_weights_results.npy', 'wb') as f:
    np.save(f, landscape)
In [14]:
import plotly.graph_objects as go
In [15]:
# Plot landscape in 3D 
a_gamma = np.arange(0, np.pi, step_size)
b_beta = np.arange(0, np.pi, step_size)
fig = go.Figure(data=go.Surface(z=landscape, x=a_gamma, y=b_beta))

fig.update_traces(contours_z=dict(show=True, usecolormap=True, highlightcolor='limegreen', project_z=True))


fig.update_layout(title="QAOA MaxCut", scene=dict(
    xaxis_title="gamma",
    yaxis_title="beta",
    zaxis_title="mean"
))
In [16]:
# Plot Heatmap 
fig = go.Figure(data=go.Heatmap(z=landscape, x=b_beta, y=a_gamma, type = 'heatmap', colorscale = 'viridis'))

# Update Layout
fig.update_layout(title="QAOA MaxCut", width=700, height=700, xaxis_title="beta", yaxis_title="gamma")

# Display Global Minimium 
fig.add_trace(
    go.Scatter(mode="markers", x=[opt_beta], y=[opt_gamma], marker_symbol=[204], text = [landscape[gamma,beta]],
                   marker_color="red",  hovertemplate="x: %{x}<br>y: %{y}<br> z: %{text:.2f}<extra></extra>", 
                   marker_line_width=1, marker_size=16))
In [17]:
# Display Optimizer Results

# Display path 
#fig.add_trace(
#    go.Scatter(mode="lines", x=gammas, y=betas, marker_symbol=[200],
#                   marker_color="white", marker_line_width=1, marker_size=8)
#)

# Display start point
#fig.add_trace(
#    go.Scatter(mode="markers", x=[gammas[0]], y=[betas[0]], marker_symbol=[204],
#                   marker_color="gray", 
#                   marker_line_width=1, marker_size=16))

# Display end point
#fig.add_trace(
#    go.Scatter(mode="markers", x=[gammas[-1]], y=[betas[-1]], marker_symbol=[204],
#                   marker_color="green", 
#                   marker_line_width=1, marker_size=16))
In [18]:
# Plot Optimizer History
#fig = go.Figure(data=go.Scatter(x=counts, y=values))
#fig.update_layout(xaxis_title="Evaluation Counts", yaxis_title="Evaluated Mean", title="Optimizer")
#fig.show()